
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>词法语法分析 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-codeblock-label/block.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-click-reveal/click_reveal.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../../styles/website.css">
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="part1-1-task.html" />
    
    
    <link rel="prev" href="part0-intro.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    实验简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../log.html">
            
                <a href="../log.html">
            
                    
                    更新日志
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">零：前置准备</li>
        
        
    
        <li class="chapter " data-level="2.1" >
            
                <span>
            
                    
                    配环境、跑测试
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../lab0/env.html">
            
                <a href="../lab0/env.html">
            
                    
                    环境配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../lab0/testing.html">
            
                <a href="../lab0/testing.html">
            
                    
                    运行测试样例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../lab0/riscv.html">
            
                <a href="../lab0/riscv.html">
            
                    
                    RISC-V 的工具链使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">第一个编译器</li>
        
        
    
        <li class="chapter " data-level="3.1" >
            
                <span>
            
                    
                    step1：仅一个 return 的 main 函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../lab1/part1.html">
            
                <a href="../lab1/part1.html">
            
                    
                    从零开始的 lexer、parser 以及汇编生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../lab1/part2.html">
            
                <a href="../lab1/part2.html">
            
                    
                    词法语法分析工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../lab1/part3.html">
            
                <a href="../lab1/part3.html">
            
                    
                    使用中间码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" >
            
                <span>
            
                    
                    其他
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.4.1" data-path="../lab1/antlr.html">
            
                <a href="../lab1/antlr.html">
            
                    
                    ANTLR 使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4.2" data-path="../lab1/visitor.html">
            
                <a href="../lab1/visitor.html">
            
                    
                    Visitor 模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4.3" data-path="../lab1/ir.html">
            
                <a href="../lab1/ir.html">
            
                    
                    IR 简明介绍
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="../lab1/spec.html">
            
                <a href="../lab1/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">常量表达式</li>
        
        
    
        <li class="chapter " data-level="4.1" >
            
                <span>
            
                    
                    step2：一元操作
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../lab2/intro.html">
            
                <a href="../lab2/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../lab2/guide.html">
            
                <a href="../lab2/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.3" data-path="../lab2/spec.html">
            
                <a href="../lab2/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.2" >
            
                <span>
            
                    
                    step3：加减乘除模
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.2.1" data-path="../lab3/intro.html">
            
                <a href="../lab3/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2.2" data-path="../lab3/guide.html">
            
                <a href="../lab3/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2.3" data-path="../lab3/spec.html">
            
                <a href="../lab3/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.3" >
            
                <span>
            
                    
                    step4：比较和逻辑表达式
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.3.1" data-path="../lab4/intro.html">
            
                <a href="../lab4/intro.html">
            
                    
                    任务概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.2" data-path="../lab4/guide.html">
            
                <a href="../lab4/guide.html">
            
                    
                    实验指导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.3" data-path="../lab4/spec.html">
            
                <a href="../lab4/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">变量和语句</li>
        
        
    
        <li class="chapter " data-level="5.1" >
            
                <span>
            
                    
                    step5：局部变量和赋值
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.1" data-path="part0-intro.html">
            
                <a href="part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="5.1.2" data-path="part1-parse.html">
            
                <a href="part1-parse.html">
            
                    
                    词法语法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.3" data-path="part1-1-task.html">
            
                <a href="part1-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.4" data-path="part4-codegen.html">
            
                <a href="part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.5" data-path="part4-1-task.html">
            
                <a href="part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.6" data-path="summary.html">
            
                <a href="summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.7" data-path="spec.html">
            
                <a href="spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5.2" >
            
                <span>
            
                    
                    step6：if 语句和条件表达式
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.2.1" data-path="../lab6/part0-intro.html">
            
                <a href="../lab6/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2.2" data-path="../lab6/part1-parse.html">
            
                <a href="../lab6/part1-parse.html">
            
                    
                    词法语法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2.3" data-path="../lab6/part1-1-task.html">
            
                <a href="../lab6/part1-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2.4" data-path="../lab6/part4-codegen.html">
            
                <a href="../lab6/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2.5" data-path="../lab6/part4-1-task.html">
            
                <a href="../lab6/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2.6" data-path="../lab6/summary.html">
            
                <a href="../lab6/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2.7" data-path="../lab6/spec.html">
            
                <a href="../lab6/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">块语句和作用域和更多语句</li>
        
        
    
        <li class="chapter " data-level="6.1" >
            
                <span>
            
                    
                    step7：块语句
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1.1" data-path="../lab7/part0-intro.html">
            
                <a href="../lab7/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.2" data-path="../lab7/part1-parse.html">
            
                <a href="../lab7/part1-parse.html">
            
                    
                    词法语法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.3" data-path="../lab7/part1-1-task.html">
            
                <a href="../lab7/part1-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.4" data-path="../lab7/part4-codegen.html">
            
                <a href="../lab7/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.5" data-path="../lab7/part4-1-task.html">
            
                <a href="../lab7/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.6" data-path="../lab7/summary.html">
            
                <a href="../lab7/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.7" data-path="../lab7/spec.html">
            
                <a href="../lab7/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6.2" >
            
                <span>
            
                    
                    step8：循环语句
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.2.1" data-path="../lab8/part0-intro.html">
            
                <a href="../lab8/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.2" data-path="../lab8/part1-parse.html">
            
                <a href="../lab8/part1-parse.html">
            
                    
                    词法语法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.3" data-path="../lab8/part1-1-task.html">
            
                <a href="../lab8/part1-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.4" data-path="../lab8/part4-codegen.html">
            
                <a href="../lab8/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.5" data-path="../lab8/part4-1-task.html">
            
                <a href="../lab8/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.6" data-path="../lab8/summary.html">
            
                <a href="../lab8/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.7" data-path="../lab8/spec.html">
            
                <a href="../lab8/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">函数和全局变量</li>
        
        
    
        <li class="chapter " data-level="7.1" >
            
                <span>
            
                    
                    step9：函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.1" data-path="../lab9/part0-intro.html">
            
                <a href="../lab9/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.2" data-path="../lab9/part1-parser.html">
            
                <a href="../lab9/part1-parser.html">
            
                    
                    词法语法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.3" data-path="../lab9/part1-1-task.html">
            
                <a href="../lab9/part1-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.4" data-path="../lab9/part4-1-cconv.html">
            
                <a href="../lab9/part4-1-cconv.html">
            
                    
                    调用约定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.5" data-path="../lab9/part4-2-codegen.html">
            
                <a href="../lab9/part4-2-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.6" data-path="../lab9/part4-3-task.html">
            
                <a href="../lab9/part4-3-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.7" data-path="../lab9/summary.html">
            
                <a href="../lab9/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.8" data-path="../lab9/spec.html">
            
                <a href="../lab9/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.2" >
            
                <span>
            
                    
                    step10：全局变量
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.2.1" data-path="../lab10/part0-intro.html">
            
                <a href="../lab10/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.2" data-path="../lab10/part1-parser.html">
            
                <a href="../lab10/part1-parser.html">
            
                    
                    词法语法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.3" data-path="../lab10/part1-1-task.html">
            
                <a href="../lab10/part1-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.4" data-path="../lab10/part4-codegen.html">
            
                <a href="../lab10/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.5" data-path="../lab10/part4-1-task.html">
            
                <a href="../lab10/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.6" data-path="../lab10/summary.html">
            
                <a href="../lab10/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.7" data-path="../lab10/spec.html">
            
                <a href="../lab10/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">指针和数组</li>
        
        
    
        <li class="chapter " data-level="8.1" >
            
                <span>
            
                    
                    step11：指针
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1.1" data-path="../lab11/part0-intro.html">
            
                <a href="../lab11/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.2" data-path="../lab11/typeck.html">
            
                <a href="../lab11/typeck.html">
            
                    
                    类型检查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.3" data-path="../lab11/part4-codegen.html">
            
                <a href="../lab11/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.4" data-path="../lab11/part4-1-task.html">
            
                <a href="../lab11/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.5" data-path="../lab11/summary.html">
            
                <a href="../lab11/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.6" data-path="../lab11/spec.html">
            
                <a href="../lab11/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8.2" >
            
                <span>
            
                    
                    step12：数组
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.2.1" data-path="../lab12/part0-intro.html">
            
                <a href="../lab12/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2.2" data-path="../lab12/part4-codegen.html">
            
                <a href="../lab12/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2.3" data-path="../lab12/part4-1-task.html">
            
                <a href="../lab12/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2.4" data-path="../lab12/summary.html">
            
                <a href="../lab12/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2.5" data-path="../lab12/spec.html">
            
                <a href="../lab12/spec.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">参考资料</li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../../REFERENCE.html">
            
                <a href="../../REFERENCE.html">
            
                    
                    参考资料
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >词法语法分析</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x8BCD;&#x6CD5;&#x8BED;&#x6CD5;&#x89E3;&#x6790;">&#x8BCD;&#x6CD5;&#x8BED;&#x6CD5;&#x89E3;&#x6790;</h1>
<h3 id="&#x652F;&#x6301;&#x591A;&#x6761;&#x8BED;&#x53E5;">&#x652F;&#x6301;&#x591A;&#x6761;&#x8BED;&#x53E5;</h3>
<p>&#x4ECE;&#x672C;&#x6B65;&#x9AA4;&#x5F00;&#x59CB;&#xFF0C;&#x51FD;&#x6570;&#x4E2D;&#x53EF;&#x80FD;&#x5177;&#x6709;&#x591A;&#x6761;&#x8BED;&#x53E5;&#xFF0C;&#x800C;&#x4E0D;&#x518D;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x8FD4;&#x56DE;&#x8BED;&#x53E5;&#x3002;&#x76F8;&#x5173;&#x7B26;&#x53F7;&#x7684;&#x5B9A;&#x4E49;&#x53D8;&#x66F4;&#x4E3A;&#xFF1A;</p>
<pre><code>&lt;function&gt; ::= &quot;int&quot; &quot;main&quot; &quot;(&quot; &quot;)&quot; &quot;{&quot; { &lt;statement&gt; } &quot;}&quot;
&lt;statement&gt; ::= &quot;return&quot; &lt;exp&gt; &quot;;&quot;
              | &lt;exp&gt; &quot;;&quot;
</code></pre><p>&#x6587;&#x6CD5;&#x4E2D;&#x7684; <code>{ &lt;statement&gt; }</code> &#x8BB0;&#x53F7;&#x8868;&#x793A; <code>&lt;statement&gt;</code> &#x53EF;&#x91CD;&#x590D; 0 &#x81F3;&#x4EFB;&#x610F;&#x591A;&#x6B21;&#x3002;&#x5728;&#x8BED;&#x6CD5;&#x89E3;&#x6790;&#x5668;&#x7684;&#x5B9E;&#x9645;&#x5B9E;&#x73B0;&#x4E2D;&#xFF0C;&#x4E5F;&#x53EF;&#x5C06;&#x4E0A;&#x8FF0;&#x6587;&#x6CD5;&#x5199;&#x4F5C;&#xFF1A;</p>
<pre><code>&lt;function&gt; ::= &quot;int&quot; &quot;main&quot; &quot;(&quot; &quot;)&quot; &quot;{&quot; &lt;statements&gt; &quot;}&quot;
&lt;statements&gt; ::= /* empty */
               | &lt;statements&gt; &lt;statement&gt;
</code></pre><p>&#x5982;&#x679C;&#x4F60;&#x9009;&#x62E9;&#x81EA;&#x5DF1;&#x4EB2;&#x81EA;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x8BED;&#x6CD5;&#x89E3;&#x6790;&#x5668;&#xFF0C;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#x540E;&#x4E00;&#x79CD;&#x6587;&#x6CD5;&#x4EE5;&#x7B80;&#x5316;&#x5B9E;&#x73B0;&#x3002;&#x5982;&#x679C;&#x4F60;&#x4F7F;&#x7528;&#x4E86; antlr &#x7B49;&#x89E3;&#x6790;&#x5668;&#x751F;&#x6210;&#x5DE5;&#x5177;&#xFF0C;&#x53EF;&#x4EE5;&#x5C1D;&#x8BD5;&#x76F4;&#x63A5;&#x7F16;&#x5199;&#x524D;&#x4E00;&#x79CD;&#x6587;&#x6CD5;&#xFF0C;&#x4F46;&#x5E76;&#x4E0D;&#x4E00;&#x5B9A;&#x6BD4;&#x540E;&#x4E00;&#x79CD;&#x6587;&#x6CD5;&#x66F4;&#x65B9;&#x4FBF;&#x3002;&#x8BF7;&#x6839;&#x636E;&#x5B9E;&#x9645;&#x60C5;&#x51B5;&#x505A;&#x51FA;&#x9009;&#x62E9;&#x3002;</p>
<h3 id="&#x6807;&#x8BC6;&#x7B26;">&#x6807;&#x8BC6;&#x7B26;</h3>
<p>&#x5728;&#x4E4B;&#x524D;&#x7684;&#x6B65;&#x9AA4;&#x4E2D;&#xFF0C;&#x552F;&#x4E00;&#x7684;&#x6807;&#x8BC6;&#x7B26;&#x2014;&#x2014;&#x4E3B;&#x51FD;&#x6570;&#x540D; <code>main</code> &#x662F;&#x56FA;&#x5B9A;&#x7684;&#x3002;&#x4E3A;&#x4E86;&#x7B80;&#x6D01;&#x8D77;&#x89C1;&#xFF0C;&#x6211;&#x4EEC;&#x5C06; <code>main</code> &#x89C6;&#x4E3A;&#x4E00;&#x4E2A;&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x786C;&#x7F16;&#x7801;&#x5728;&#x8BCD;&#x6CD5;&#x548C;&#x8BED;&#x6CD5;&#x89E3;&#x6790;&#x5668;&#x4E2D;&#x3002;&#x4F46;&#x4ECE;&#x672C;&#x6B65;&#x9AA4;&#x5F00;&#x59CB;&#xFF0C;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x540D;&#x79F0;&#x5C06;&#x662F;&#x4E0D;&#x5B9A;&#x7684;&#xFF0C;&#x6545;&#x9700;&#x8981;&#x5F15;&#x5165;&#x4E00;&#x4E2A;&#x4E13;&#x95E8;&#x8868;&#x793A;&#x6807;&#x8BC6;&#x7B26;&#x7684;&#x6807;&#x8BB0; <code>Identifier</code>&#xFF0C;&#x5B9A;&#x4E49;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>Identifier [a-zA-Z_][a-zA-Z0-9_]*
</code></pre><p>&#x800C;&#x51FD;&#x6570;&#x7B26;&#x53F7;&#x7684;&#x5B9A;&#x4E49;&#x5219;&#x8FDB;&#x4E00;&#x6B65;&#x53D8;&#x66F4;&#x4E3A;&#xFF1A;</p>
<pre><code>&lt;function&gt; ::= &quot;int&quot; Identifier &quot;(&quot; &quot;)&quot; &quot;{&quot; { &lt;statement&gt; } &quot;}&quot;
</code></pre><h3 id="&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x5B9A;&#x4E49;">&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x5B9A;&#x4E49;</h3>
<p>&#x8BED;&#x53E5;&#x7B26;&#x53F7;&#x7684;&#x5B9A;&#x4E49;&#x505A;&#x5982;&#x4E0B;&#x8FDB;&#x4E00;&#x6B65;&#x4FEE;&#x6539;&#xFF0C;&#x5C06;&#x652F;&#x6301;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x5B9A;&#x4E49;&#x548C;&#x521D;&#x59CB;&#x5316;&#x89C6;&#x4F5C;&#x4E00;&#x79CD;&#x8BED;&#x53E5;&#xFF1A;</p>
<pre><code>&lt;statement&gt; ::= &quot;return&quot; &lt;exp&gt; &quot;;&quot;
              | &lt;exp&gt; &quot;;&quot;
              | &quot;int&quot; Identifier [ &quot;=&quot; &lt;exp&gt;] &quot;;&quot;  // &lt;- &#x770B;&#x8FD9;&#x91CC;
</code></pre><p>&#x5176;&#x4E2D; <code>[ = &lt;exp&gt;]</code> &#x8868;&#x793A;&#x521D;&#x59CB;&#x5316;&#x8868;&#x8FBE;&#x5F0F;&#x662F;&#x53EF;&#x9009;&#x7684;&#x3002;&#x6839;&#x636E;&#x4F60;&#x5B9E;&#x73B0;&#x89E3;&#x6790;&#x5668;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5C06;&#x4E0A;&#x8FF0;&#x6587;&#x6CD5;&#x6539;&#x5199;&#x6210;&#xFF1A;</p>
<pre><code>&lt;statement&gt; ::= &quot;return&quot; &lt;exp&gt; &quot;;&quot;
              | &lt;exp&gt; &quot;;&quot;
              | &quot;int&quot; Identifier &quot;;&quot;
              | &quot;int&quot; Identifier &quot;=&quot; &lt;exp&gt; &quot;;&quot;
</code></pre><p>&#x89E3;&#x6790;&#x5668;&#x5E94;&#x751F;&#x6210;&#x76F8;&#x5E94;&#x7684; AST &#x7ED3;&#x70B9;&#xFF1A;</p>
<pre><code>statement = Return(exp)
          | Declare(string, exp option)  // &lt;- &#x770B;&#x8FD9;&#x91CC;
          | Exp(exp)
</code></pre><p>&#x5176;&#x4E2D; <code>string</code> &#x8868;&#x793A;&#x53D8;&#x91CF;&#x540D;&#xFF0C;&#x800C; <code>exp</code> &#x662F;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x53C2;&#x6570;&#xFF0C;&#x8868;&#x793A;&#x53D8;&#x91CF;&#x7684;&#x521D;&#x59CB;&#x5316;&#x8868;&#x8FBE;&#x5F0F;&#x3002;</p>
<h3 id="&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x8BBF;&#x95EE;&#x548C;&#x8D4B;&#x503C;">&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x8BBF;&#x95EE;&#x548C;&#x8D4B;&#x503C;</h3>
<p>&#x8868;&#x8FBE;&#x5F0F;&#x7B26;&#x53F7;&#x505A;&#x5982;&#x4E0B;&#x4FEE;&#x6539;&#xFF1A;</p>
<pre><code>&lt;exp&gt; ::= Identifier &quot;=&quot; &lt;exp&gt; | &lt;logical-or-exp&gt;  // &lt;- &#x770B;&#x8FD9;&#x91CC;
&lt;logical-or-exp&gt; ::= &lt;logical-and-exp&gt; { &quot;||&quot; &lt;logical-and-exp&gt; }
&lt;logical-and-exp&gt; ::= &lt;equality-exp&gt; { &quot;&amp;&amp;&quot; &lt;equality-exp&gt; }
&lt;equality-exp&gt; ::= &lt;relational-exp&gt; { (&quot;!=&quot; | &quot;==&quot;) &lt;relational-exp&gt; }
&lt;relational-exp&gt; ::= &lt;additive-exp&gt; { (&quot;&lt;&quot; | &quot;&gt;&quot; | &quot;&lt;=&quot; | &quot;&gt;=&quot;) &lt;additive-exp&gt; }
&lt;additive-exp&gt; ::= &lt;term&gt; { (&quot;+&quot; | &quot;-&quot;) &lt;term&gt; }
&lt;term&gt; ::= &lt;factor&gt; { (&quot;*&quot; | &quot;/&quot;) &lt;factor&gt; }
&lt;factor&gt; ::= &quot;(&quot; &lt;exp&gt; &quot;)&quot; | &lt;unary_op&gt; &lt;factor&gt; | Integer | Identifier  // &lt;- &#x770B;&#x8FD9;&#x91CC;
&lt;unary_op&gt; ::= &quot;!&quot; | &quot;~&quot; | &quot;-&quot;
</code></pre><p>&#x5F53; <code>&lt;factor&gt;</code> &#x662F;&#x4E00;&#x4E2A; <code>Identifier</code> &#x65F6;&#xFF0C;&#x8868;&#x793A;&#x8BBF;&#x95EE;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x3002;&#x5F53; <code>&lt;exp&gt;</code> &#x662F; <code>Identifier &quot;=&quot; &lt;exp&gt;</code> &#x65F6;&#xFF0C;&#x8868;&#x793A;&#x5BF9;&#x53D8;&#x91CF;&#x8D4B;&#x503C;&#x3002;</p>
<p>&#x6700;&#x540E;&#xFF0C;&#x89E3;&#x6790;&#x5668;&#x5E94;&#x751F;&#x6210;&#x76F8;&#x5E94;&#x7684; AST &#x7ED3;&#x70B9;&#xFF1A;</p>
<pre><code>exp = Assign(string, exp)  // &lt;- &#x770B;&#x8FD9;&#x91CC;
    | Var(string)  // &lt;- &#x770B;&#x8FD9;&#x91CC;
    | BinOp(binary_operator, exp, exp)
    | UnOp(unary_operator, exp)
    | Constant(int)
</code></pre><p>&#x5176;&#x4E2D; <code>string</code> &#x8868;&#x793A;&#x53D8;&#x91CF;&#x540D;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="part0-intro.html" class="navigation navigation-prev " aria-label="Previous page: 摘要">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="part1-1-task.html" class="navigation navigation-next " aria-label="Next page: 任务">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"词法语法分析","level":"5.1.2","depth":2,"next":{"title":"任务","level":"5.1.3","depth":2,"path":"docs/lab5/part1-1-task.md","ref":"docs/lab5/part1-1-task.md","articles":[]},"previous":{"title":"摘要","level":"5.1.1","depth":2,"path":"docs/lab5/part0-intro.md","ref":"docs/lab5/part0-intro.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-element","chapter-fold","katex","alerts","emphasize","mermaid-gb3","codeblock-label","code","search-pro","click-reveal","expandable-chapters-interactive","localized-footer"],"pluginsConfig":{"chapter-fold":{},"emphasize":{},"codeblock-label":{},"search-pro":{},"search":{},"localized-footer":{"filename":"gitalk.html","hline":"true"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"hide-element":{},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":1},"click-reveal":{},"highlight":{},"mermaid-gb3":{},"expandable-chapters-interactive":{},"alerts":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"docs/lab5/part1-parse.md","mtime":"2020-09-02T07:27:37.423Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-09-02T08:48:42.551Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-click-reveal/click_reveal.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

